package com.z1.core.io;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

import com.z1.core.exception.Z1Exception;

public class IOHelper {

	/**
	 * 根据序列化文件读取序列化对象
	 * @param file
	 * @return
	 */
	public static final Serializable readSerializable(File file) {
		FileInputStream fis = null;
		ObjectInputStream ois = null;
		Serializable ser = null;
		try {
			if (null != file && file.exists() && file.isFile()) {
				fis = new FileInputStream(file);
				ois = new ObjectInputStream(fis);
				ser = (Serializable)ois.readObject();
			}
		}
		catch (Throwable e) {
			throw new Z1Exception(e);
		}
		finally {
			try {
				if (null != fis) { fis.close(); }
				if (null != ois) { ois.close(); }
			}
			catch (Exception ex){}
		}
		return ser;
	}
	/**
	 * 根据序列化文件路径读取序列化对象
	 * @param path
	 * @return
	 * @throws Exception 
	 */
	public static final Serializable readSerializable(String path) {
		return readSerializable(new File(path));
	}
	/**
	 * 根据序列化文件写入序列化对象
	 * @param file
	 * @param ser
	 * @throws Exception 
	 */
	public static final void writeSerializable(File file, Serializable ser) {
		FileOutputStream fos = null;
		ObjectOutputStream oos = null;
		try {
			if (!file.getParentFile().exists()) {
				file.getParentFile().mkdirs();
			}
			if (file.exists()) {
				file.delete();
			}
			file.createNewFile();
			fos = new FileOutputStream(file);
			oos = new ObjectOutputStream(fos);
			oos.writeObject(ser);
			oos.flush();
		}
		catch (Throwable e) {
			throw new Z1Exception(e);
		}
		finally {
			try {
				if (null != fos) { fos.close(); }
				if (null != oos) { oos.close(); }
			}
			catch (Exception ex){ex.printStackTrace();}
		}
	}
	/**
	 * 根据序列化文件路径写入序列化对象
	 * @param path
	 * @param ser
	 * @throws Exception 
	 */
	public static final void writeSerializable(String path, Serializable ser) {
		writeSerializable(new File(path), ser);
	}
	
	/**
	 * 读取指定文件二进制数据
	 * @param file
	 * @param ser
	 * @throws Exception 
	 */
	public static final byte[] readBytes(File file) {
		byte[] buffer = new byte[0];
		if (null != file && file.exists() && file.isFile()) {
			InputStream in = null;
			try {
				int len = (int)file.length();
				buffer = new byte[len];
				in = new FileInputStream(file);
				in.read(buffer);
			}
			catch (Throwable e) {}
			finally {
				if (null != in) {
					try {in.close();}
					catch (IOException e) {}
				} 
			}
		}
		return buffer;
	}
	public static final byte[] readBytes(String path) {
		return readBytes(new File(path));
	}
	
	/**
	 * 二进制数据写文件
	 * @param file
	 * @param buffer
	 */
	public static final void writeBytes(File file, byte[] buffer) {
		if (null != file && null != buffer) {
			if (file.exists() && file.isFile()) {
				file.delete();
			}
			FileCreator.fileCreator(file);
			FileOutputStream out = null;
			try {
				out = new FileOutputStream(file);
				out.write(buffer);
			}
			catch (Throwable e) {
				throw new Z1Exception(e);
			}
			finally {
				if (null != out) {
					try {out.close();}
					catch (IOException e) {}
				}
			}
		}
	}
	public static final void writeBytes(String path, byte[] buffer) {
		writeBytes(new File(path), buffer);
	}
	
	/**
	 * 复制文件
	 * @param srcFile 源文件
	 * @param targetFile 目的文件
	 * @throws Exception
	 */
	public static final void copyFile(File srcFile, File targetFile) {
		if (null == srcFile || !srcFile.exists() || !srcFile.isFile()) {
			return;
		}
		File dir = targetFile.getParentFile();
		if (null != dir) {
			if (!dir.exists() || !dir.isDirectory()) {
				dir.mkdirs();
			}
			if (targetFile.exists() && targetFile.isFile()) {
				targetFile.delete();
			}
			byte[] bt = new byte[1024];
			FileInputStream fis = null;
			FileOutputStream fos = null;
			try {
				fis = new FileInputStream(srcFile);
				fos = new FileOutputStream(targetFile);
				while(-1 != fis.read(bt)){
					  fos.write(bt, 0, bt.length);
				}
			}
			catch (Throwable e) {}
			finally {
				try {
					if (null != fos) {
						fos.close();
					}
					if (null != fis) {
						fis.close();
					}
				}
				catch (Exception ex){}
			}
		}
	}
	
	/**
	 * 删除文件/文件夹
	 * @param targetFile
	 */
	public static final void deleteFile(File targetFile) {
		if (null == targetFile || !targetFile.exists()) {
			return;
		}
		if (targetFile.isDirectory()) {
			for (File file : targetFile.listFiles()) {
				deleteFile(file);
			}
		}
		targetFile.delete();
	}
	
	/**
	 * 文件/文件夹重命名
	 * @param srcFile
	 * @param targetFile
	 */
	public static final void renameFile(File srcFile, File targetFile) {
		if (null == srcFile || !srcFile.exists() || !srcFile.isFile()) {
			return;
		}
		if (srcFile.isDirectory()) {
			if (!targetFile.exists() && !targetFile.isDirectory()) {
				targetFile.mkdirs();
			}
			for (File file : srcFile.listFiles()) {
				renameFile(file, new File(targetFile, file.getName()));
			}
		}
		else {
			srcFile.renameTo(targetFile);
		}
		deleteFile(srcFile);
	}
	
	public static void main(String[] args) throws Exception {
		writeSerializable(new File("C:/TEST"), new String());
	}
	
}
